<?php
session_start();

// 初始化游戏地图和玩家信息
if (!isset($_SESSION['game'])) {
    $_SESSION['game'] = [
        'map' => [
            ['#', '#', '#', '#', '#'],
            ['#', ' ', ' ', ' ', '#'],
            ['#', ' ', '@', ' ', '#'],
            ['#', ' ', ' ', ' ', '#'],
            ['#', '#', '#', '#', '#']
        ],
        'player_x' => 2,
        'player_y' => 2,
        'bomb_range' => 2
    ];
}

// 处理玩家移动
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $action = $_POST['action'];
    $game = &$_SESSION['game'];
    $x = $game['player_x'];
    $y = $game['player_y'];

    switch ($action) {
        case 'up':
            if ($game['map'][$y - 1][$x] === ' ') {
                $game['map'][$y][$x] = ' ';
                $game['player_y']--;
                $game['map'][$game['player_y']][$game['player_x']] = '@';
            }
            break;
        case 'down':
            if ($game['map'][$y + 1][$x] === ' ') {
                $game['map'][$y][$x] = ' ';
                $game['player_y']++;
                $game['map'][$game['player_y']][$game['player_x']] = '@';
            }
            break;
        case 'left':
            if ($game['map'][$y][$x - 1] === ' ') {
                $game['map'][$y][$x] = ' ';
                $game['player_x']--;
                $game['map'][$game['player_y']][$game['player_x']] = '@';
            }
            break;
        case 'right':
            if ($game['map'][$y][$x + 1] === ' ') {
                $game['map'][$y][$x] = ' ';
                $game['player_x']++;
                $game['map'][$game['player_y']][$game['player_x']] = '@';
            }
            break;
        case 'bomb':
            $range = $game['bomb_range'];
            // 简单模拟炸弹爆炸效果
            for ($i = -$range; $i <= $range; $i++) {
                if ($y + $i >= 0 && $y + $i < count($game['map'])) {
                    if ($game['map'][$y + $i][$x] === '#') {
                        $game['map'][$y + $i][$x] = ' ';
                    }
                }
            }
            for ($i = -$range; $i <= $range; $i++) {
                if ($x + $i >= 0 && $x + $i < count($game['map'][0])) {
                    if ($game['map'][$y][$x + $i] === '#') {
                        $game['map'][$y][$x + $i] = ' ';
                    }
                }
            }
            break;
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>炸弹人网页游戏</title>
    <style>
        table {
            border-collapse: collapse;
        }

        td {
            width: 30px;
            height: 30px;
            border: 1px solid #000;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>炸弹人网页游戏</h1>
    <table>
        <?php
        $game = $_SESSION['game'];
        foreach ($game['map'] as $row) {
            echo '<tr>';
            foreach ($row as $cell) {
                echo '<td>'. $cell. '</td>';
            }
            echo '</tr>';
        }
        ?>
    </table>
    <form method="post">
        <input type="submit" name="action" value="up" />
        <input type="submit" name="action" value="down" />
        <input type="submit" name="action" value="left" />
        <input type="submit" name="action" value="right" />
        <input type="submit" name="action" value="bomb" />
    </form>
</body>

</html>